// my/wx.js
import {
  ConfirmModel
} from '../../models/confirm'
const confirmModel = new ConfirmModel()
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    my: {},
    arrowType: 'arrow-down',
    oilList: '',
    oilCode: '',
    oilName: '',
    selectIndex: 0,
    showOilPop: false,
    stationList: '',
    locationAddress: app.globalData.userLocation,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    console.log(app.globalData)
    this.getGoodsList()
  },
  openoilPop () {
    if(this.data.arrowType == 'arrow-down') {
      this.setData({
        arrowType: 'arrow-up',
        showOilPop: true,
      })
    } else {
      this.setData({
        arrowType: 'arrow-down',
        showOilPop: false,
      })

    }
  },
  selectOil(e) {
    const selectIndex = e.currentTarget.dataset.index
    const selectOil = this.data.oilList[selectIndex]
    console.log(selectOil)
    this.setData({
      selectIndex: selectIndex,
      oilCode: selectOil.oilCode,
      oilName: selectOil.oilName,
      showOilPop: false,
      arrowType: 'arrow-down',
    })
    if (app.globalData.userLocation) {
      this.setData({
        locationAddress: app.globalData.userLocation,
      })
      this.getStationList(this.data.oilCode,this.data.value,app.globalData.userLocation.userX,app.globalData.userLocation.userY)
    } else {
      this.getStationList(this.data.oilCode,this.data.value,'','')
      this.setData({
        locationAddress: '',
      })
    }
  },
  //  获取油品
  getGoodsList() {
    confirmModel.getGoodsList().then(res => {
      console.log(res)
      if (res && res.length >0) {
        console.log('')
        this.setData({
          oilCode: res[0].oilCode,
          oilName: res[0].oilName,
        })
        this.setData({
          oilList: res,
        })
        if (app.globalData.userLocation) {
          this.setData({
            locationAddress: app.globalData.userLocation,
          })
          this.getStationList(this.data.oilCode,'',app.globalData.userLocation.userX,app.globalData.userLocation.userY)
        } else {
          this.getStationList(this.data.oilCode,'','','')
          this.setData({
            locationAddress: '',
          })
        }
      } else {
        wx.showToast({
          type: 'none',
          content: '获取油品列表失败，请重试',
          duration: 600,
        });
      }
    });
  },
  //  获取油站
  getStationList(oilCode,stationName,userX,userY) {
    confirmModel.getStationList(oilCode,stationName, userX, userY).then(res => {
      console.log(res)
      if (res ) {
        this.setData({
          stationList: res,
        })
      } else {
        wx.showToast({
          type: 'none',
          content: '获取油站列表失败，请重试',
          duration: 600,
        });
      }
    });
  },
  relocation() {
    let vm = this
     wx.getLocation({type: 3,
        success(res) {
          console.log(res)
          let userLocation = {
            userX: res.longitude,
            userY: res.latitude
          }
          if (res.district && res.pois && res.pois[0]) {
            userLocation.address = res.district + res.pois[0].name
          } else {
            userLocation.address = '获取当前位置失败'
          }
          app.globalData.userLocation = userLocation
          vm.setData({
              locationAddress: app.globalData.userLocation,
          })
          vm.getStationList(vm.data.oilCode,'',app.globalData.userLocation.userX,app.globalData.userLocation.userY)
        },
        fail: (error) => {
          console.log('fail')
          console.log(error)
          wx.alert({
            content: error.errorMessage,
            buttonText: '查看附近油站',
            success: () => {
            }
          });
        },
      })
  },
  handleClear(value) {
    this.setData({
      value: '',
    });
  },
  handleInput(value) {
    this.setData({
      value,
    });
    setTimeout(() => {
      if (app.globalData.userLocation) {
        this.getStationList(this.data.oilCode,this.data.value,app.globalData.userLocation.userX,app.globalData.userLocation.userY)
      } else {
        this.getStationList(this.data.oilCode,this.data.value,'','')
      }
    }, 300);
    
  },
  handleBlur() {
    // wx.alert({
    //   content: '~~' + this.data.value,
    // });
    // if (app.globalData.userLocation) {
    //   this.getStationList(this.data.oilCode,this.data.value,app.globalData.userLocation.userX,app.globalData.userLocation.userY)
    // } else {
    //   this.getStationList(this.data.oilCode,this.data.value,'','')
    // }
  },
  handleSubmit(value) {
    // this.handleBlur()
    // wx.alert({
    //   content: value,
    // });
    // if (app.globalData.userLocation) {
    //   this.getStationList(this.data.oilCode,value,app.globalData.userLocation.userX,app.globalData.userLocation.userY)
    // } else {
    //   this.getStationList(this.data.oilCode,value,'','')
    // }
  },
  selectStation (e) {
    console.log(e)
    let station = {
      alipay_store_id: e.target.dataset.item.shopId,
      // alipay_store_id:'2019111900077000000084753577',  // test1 2019112600077000000084989653
      store_id: e.target.dataset.item.id,
      oil_num: this.data.oilName,
      myjy_source: 'mayijiayou',
      stationX:  e.target.dataset.item.posX,
      stationY:  e.target.dataset.item.posY,
    }
    app.globalData.station = station
    console.log(app.globalData.station )
    // wx.navigateBack()
    wx.reLaunch({
      url: '/pages/confirm/confirm'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})